<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC3040 – In POSIX sh, set option *[name]* is undefined.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC3040 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC3040">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2 id="in-posix-sh-set-option-name-is-undefined">In POSIX sh, set
option <em>[name]</em> is undefined.</h2>
<p>The value <em>[name]</em> is something other than
<code>allexport</code>, <code>errexit</code>, <code>ignoreeof</code>,
<code>monitor</code>, <code>noclobber</code>, <code>noexec</code>,
<code>noglob</code>, <code>nolog</code>, <code>notify</code> ,
<code>nounset</code>, <code>verbose</code>, <code>vi</code>, or
<code>xtrace</code>.</p>
<!-- https://github.com/koalaman/shellcheck/blob/3b6972fbf14d98dded7ebfc65af6b73724be4efa/src/ShellCheck/Checks/ShellSupport.hs#L334-L337 -->
<h3 id="rationale">Rationale:</h3>
<p>In a script declared to run with <code>sh</code> or
<code>dash</code>, ShellCheck found <code>set -o</code> used with an
option that is not defined by POSIX. The option may not be supported by
these shells.</p>
<h3 id="exceptions">Exceptions:</h3>
<p>If the script has already inspected the current shell and determined
the option is available, you can <a href="ignore.html">ignore</a> this
message.</p>
<h3 id="pipefail"><code>pipefail</code></h3>
<h4 id="problematic-code">Problematic code:</h4>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC3040.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co">#!/bin/sh</span></span>
<span id="cb1-2"><a href="SC3040.html#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="bu">set</span> <span class="at">-o</span> pipefail</span>
<span id="cb1-3"><a href="SC3040.html#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="ex">cmd1</span> <span class="kw">|</span> <span class="ex">cmd2</span> <span class="kw">|</span> <span class="ex">cmd3</span></span>
<span id="cb1-4"><a href="SC3040.html#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="cf">then</span></span>
<span id="cb1-5"><a href="SC3040.html#cb1-5" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Success&quot;</span></span>
<span id="cb1-6"><a href="SC3040.html#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="cf">fi</span></span></code></pre></div>
<h4 id="correct-code">Correct code:</h4>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC3040.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="va">fail</span><span class="op">=</span><span class="st">&quot;</span><span class="va">$(</span><span class="fu">mktemp</span><span class="va">)</span><span class="st">&quot;</span></span>
<span id="cb2-2"><a href="SC3040.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="kw">{</span> <span class="ex">cmd1</span> <span class="kw">||</span> <span class="bu">echo</span> <span class="op">&gt;</span> <span class="st">&quot;</span><span class="va">$fail</span><span class="st">&quot;</span><span class="kw">;</span> <span class="kw">}</span> <span class="kw">|</span></span>
<span id="cb2-3"><a href="SC3040.html#cb2-3" aria-hidden="true" tabindex="-1"></a>   <span class="kw">{</span> <span class="ex">cmd2</span> <span class="kw">||</span> <span class="bu">echo</span> <span class="op">&gt;</span> <span class="st">&quot;</span><span class="va">$fail</span><span class="st">&quot;</span><span class="kw">;</span> <span class="kw">}</span> <span class="kw">|</span></span>
<span id="cb2-4"><a href="SC3040.html#cb2-4" aria-hidden="true" tabindex="-1"></a>     <span class="ex">cmd3</span> <span class="kw">&amp;&amp;</span> <span class="bu">[</span> <span class="ot">!</span> <span class="ot">-s</span> <span class="st">&quot;</span><span class="va">$fail</span><span class="st">&quot;</span> <span class="bu">]</span></span>
<span id="cb2-5"><a href="SC3040.html#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="cf">then</span></span>
<span id="cb2-6"><a href="SC3040.html#cb2-6" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Success&quot;</span></span>
<span id="cb2-7"><a href="SC3040.html#cb2-7" aria-hidden="true" tabindex="-1"></a><span class="cf">fi</span></span>
<span id="cb2-8"><a href="SC3040.html#cb2-8" aria-hidden="true" tabindex="-1"></a><span class="fu">rm</span> <span class="st">&quot;</span><span class="va">$fail</span><span class="st">&quot;</span></span></code></pre></div>
<p>Each stage in the pipeline such as <code>cmd1</code> can be rewritten
as <code>{ cmd1 || echo &gt; file; }</code> which will behave as before
but now also write the exit code into <code>file</code> if the command
fails.</p>
<p>If the file has been written to when the pipeline is done, it means
one of the commands failed. This is demonstrated in the correct
example.</p>
<p>This can obviously be extended with e.g.
<code>echo "cmd1=$?" &gt;&gt; file</code> if the particular exit codes
for particular commands are desired.</p>
<p>If <code>pipefail</code> or other options are used only as a sanity
check, another option is to try the setting in a subshell and only apply
it in the main shell if successful. This will set the flag on platforms
where <code>sh</code> is linked to <code>ash</code>,
<code>busybox</code>, or <code>bash</code>, but do nothing if
<code>sh</code> is <code>dash</code>.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC3040.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Set pipefail if it works in a subshell, disregard if unsupported</span></span>
<span id="cb3-2"><a href="SC3040.html#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="co"># shellcheck disable=SC3040</span></span>
<span id="cb3-3"><a href="SC3040.html#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="kw">(</span><span class="bu">set</span> <span class="at">-o</span> pipefail <span class="dv">2</span><span class="op">&gt;</span> /dev/null<span class="kw">)</span> <span class="kw">&amp;&amp;</span> <span class="bu">set</span> <span class="at">-o</span> pipefail</span></code></pre></div>
<p>Alternatively, switch to a shell that supports <code>pipefail</code>,
such as Bash.</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li><a
href="https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set">POSIX.1-2017
- Shell Command Language - <code>set</code></a></li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


